package ai.neuralnet.builders;

import ai.neuralnet.NNBuilder;
import ai.neuralnet.gradientDescent.GDNeuralNetwork;
import ai.neuralnet.gradientDescent.GDNeuron;

import java.util.ArrayList;

/**
 * Builds the XOR network from homework 7
 * for debugging gradient descent
 */
public class DebugNetworkBuilder extends NNBuilder<GDNeuron, GDNeuralNetwork>
{

    public DebugNetworkBuilder()
    {
        super(GDNeuron.class, GDNeuralNetwork.class);
    }

    @Override
    public GDNeuralNetwork build()
    {
        GDNeuralNetwork network = new GDNeuralNetwork(this);

        GDNeuron[] inputs = new GDNeuron[2];
        for (int i = 0; i < 2; i++)
        {
            inputs[i] = new GDNeuron(1);
            inputs[i].setIsInputNeuron(true);
        }

        network.setInputNeurons(inputs);

        ArrayList<GDNeuron> hidden = new ArrayList<GDNeuron>();

        for (int i = 0; i < 2; i++)
        {
            GDNeuron n = new GDNeuron(2);
            for (GDNeuron in : inputs)
            {
                n.addInputNeuron(in);

            }

            hidden.add(n);
        }


        for (GDNeuron in : inputs)
        {
            in.addOutputNeuron(hidden);

        }


        network.addHiddenNeuron(hidden);

        ArrayList<GDNeuron> output = new ArrayList<GDNeuron>();

        for (int i = 0; i < 3; i++)
        {
            output.add(new GDNeuron(2));

            for (GDNeuron n : hidden)
            {
                output.get(i).addInputNeuron(n);
            }
        }

        for (GDNeuron n : hidden)
        {
            n.addOutputNeuron(output);
        }

        GDNeuron[] out = new GDNeuron[3];
        output.toArray(out);
        network.setOutputNeurons(out);

        network.setWeights(0.2);
        return network;
    }
}
